﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using EnvDTE;
using Microsoft.VisualStudio.TemplateWizard;
using System.Reflection;
using System.Net;

namespace NeosSDI.MVC4.ProjectTemplateWizard
{
    public static class GlobalData
    {
    }

    public class IWizardImplementation : IWizard
    {
        private SolutionWizard solutionWizard;
        private string customMessage;

        // This method is called before opening any item that 
        // has the OpenInEditor attribute.
        public void BeforeOpeningFile(ProjectItem projectItem)
        {
        }

        public void ProjectFinishedGenerating(Project project)
        {
        }

        // This method is only called for item templates,
        // not for project templates.
        public void ProjectItemFinishedGenerating(ProjectItem
            projectItem)
        {
        }

        // This method is called after the project is created.
        public void RunFinished()
        {
        }

        public void RunStarted(object automationObject,
            Dictionary<string, string> replacementsDictionary,
            WizardRunKind runKind, object[] customParams)
        {
            try
            {

                Assembly assembly = Assembly.Load("System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
                if (assembly == null)
                {
                    MVC3NotInstalled window = new MVC3NotInstalled();
                    window.ShowDialog();
                }

                // Display a form to the user. The form collects 
                // input for the custom message.
                if (replacementsDictionary.ContainsKey("$safeprojectname$"))
                    customMessage = replacementsDictionary["$safeprojectname$"];

                string currentVersion = "1.2.4";
                if (CheckForInternetConnection())
                {
                    string WebSite = "http://visualstudiogallery.msdn.microsoft.com/3702cff6-d04e-4aca-bcd8-01d0822de9f7";
                    string webVersion = ExtractVersion(WebSite);
                    if (currentVersion != webVersion)
                    {
                        MessageBox.Show("A new Version in available. You should update your template using extension manager.");
                    }
                }

                // Add custom parameters.
                replacementsDictionary.Add("$solutionName$", customMessage);
                globalDictionary = new Dictionary<string, string>();
                globalDictionary.Add("$solutionName$",customMessage);

                //replacementsDictionary.Add("$custommessage$",
                //    "ashraful");
            }
            catch (Exception ex)
            {
                MVC3NotInstalled window = new MVC3NotInstalled();
                window.ShowDialog();
            }
        }

        private static string ExtractVersion(string WebSite)
        {
            try
            {
                using (var client = new WebClient())
                {
                    string stream = client.DownloadString(WebSite);
                    string _versionLabel = "<span id=\"Version\">";
                    string _substring = stream.Substring(stream.IndexOf(_versionLabel), 150).Trim().Replace(Environment.NewLine, "");
                    return _substring.Substring(_versionLabel.Length).Substring(0, _substring.Substring(_versionLabel.Length).IndexOf("<")).Trim();
                }
            }
            catch
            {
            }
            return "1.0";
        }

        public static bool CheckForInternetConnection()
        {
            try
            {
                using (var client = new WebClient())
                using (var stream = client.OpenRead("http://www.google.com"))
                {
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        public static Dictionary<string, string> globalDictionary;
        public static bool PackageCreated;

        // This method is only called for item templates,
        // not for project templates.
        public bool ShouldAddProjectItem(string filePath)
        {
            return true;
        }
    }
}